package com.spzx.product.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.product.domain.Brand;
import com.spzx.product.domain.CategoryBrand;
import com.spzx.product.mapper.BrandMapper;
import com.spzx.product.mapper.CategoryBrandMapper;
import com.spzx.product.service.BrandService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class BrandServiceImpl implements BrandService {
    @Resource
    BrandMapper BrandMapper;
    @Autowired
    CategoryBrandMapper categoryBrandMapper;
    @Override
    public List<Brand> selectBrandList(Brand Brand) {
        return BrandMapper.selectBrandList(Brand);
    }

    @Override
    public Brand selectBrandById(Long id) {
        return BrandMapper.selectBrandById(id);
    }

    @Override
    public int insertBrand(Brand Brand) {
        return BrandMapper.insertBrand(Brand);
    }

    @Override
    public int updateBrand(Brand Brand) {
        return BrandMapper.updateBrand(Brand);
    }

    @Override
    public int deleteBrandByIds(Long[] ids) {
        return BrandMapper.deleteBrandByIds(ids);
    }

    @Override
    public List<Brand> selectBrandAll() {
        return BrandMapper.selectBrandList(null);
    }

    @Override
    public List<Brand> selectBrandListByC3id(Long c3id) {

        List<Long> brandIds = categoryBrandMapper.selectList(new LambdaQueryWrapper<CategoryBrand>().eq(CategoryBrand::getCategoryId, c3id)
                .select(CategoryBrand::getBrandId)).stream().map(categoryBrand -> categoryBrand.getBrandId()).collect(Collectors.toList());

        if (CollectionUtils.isEmpty(brandIds)){
            throw new ServiceException("没有查询到品牌数据");
        }
      return BrandMapper.selectBrandByIds(brandIds);

    }



}
